package org.akomantoso.core.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.io.Externalizable;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.OutputUtil;
import org.akomantoso.core.visitor.AKVisitable;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * <pre>
 * 	<xsd:element name="classification">
 * 		<xsd:complexType>
 * 			<xsd:sequence>
 * 				<xsd:element ref="keyword" minOccurs="1" maxOccurs="unbounded"/>
 * 			</xsd:sequence>
 * 			<xsd:attributeGroup ref="source"/>
 * 		</xsd:complexType>
 * 	</xsd:element>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 */
public final class Classification implements org.akomantoso.core.data.attribute.Source, Externalizable, AKVisitable {

	public static final String ELEMENT = "classification";
	
	private String _source;
	
	private final List<Keyword> _keywords = new ArrayList<Keyword>();
	
	/**
	 * @see org.akomantoso.core.data.attribute.Source#getSource()
	 */
	@Override
	public String getSource() {
		return _source;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Source#setSource(java.lang.String)
	 */
	@Override
	public void setSource(String source) {
		_source = source;
	}

	public List<Keyword> getKeywords() {
		return _keywords;
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <T extends Output> void writeExternal(T out) throws IOException {
		out.writeStart(ELEMENT);
		OutputUtil.writeSource(out, this);
		for (int i = 0 , n = _keywords.size() ; i < n ; i++) {
			_keywords.get(i).writeExternal(out);
		}
		out.writeEnd();
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <T extends Input> void readExternal(T in) throws IOException {
		_source = in.readAttribute(org.akomantoso.core.data.attribute.Source.ATTRIBUTE);
		in.readNextStartElement();
		if (Keyword.ELEMENT == in.getLocalName()) {
			Keyword keyword = null;
			while (Keyword.ELEMENT == in.getLocalName()) {
				_keywords.add(keyword = new Keyword());
				keyword.readExternal(in);
				in.readNextStartElement();
			}
		}
	}

	/**
	 * @see org.akomantoso.core.visitor.AKVisitable#accept(org.akomantoso.core.visitor.AKVisitor)
	 */
	@Override
	public void accept(AKVisitor visitor) {
	}
	
}